package top.oneit.jdownload.controller;

import com.jfoenix.controls.JFXButton;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import top.oneit.jdownload.stages.dialog.AddDownload;

import java.net.URL;
import java.util.ResourceBundle;

public class HomeController extends BaseInitializable {

    @FXML
    private JFXButton createDownloadTask;
    @FXML
    private AnchorPane homeAnchorPane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        addDownload();
    }

    private void addDownload() {
        createDownloadTask.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                AddDownload stage = getStage(AddDownload.class);
                if (stage != null) {
                    // 如果被缩小到图标栏，则弹出显示
                    stage.setIconified(false);
                    stage.show();
                    // 窗口焦点
                    stage.toFront();
                    return;
                } else {
                    // 打开添加下载场景
                    new AddDownload();
                }
//getStage(HomeStage.class).homeStageObservable.set(JObservable.ADD_DOWNLOAD);


                /*HomeStage homeStage = (HomeStage) JdownloadApplication.stages.get("HomeStage");
                JFXDialog jfxDialog=new JFXDialog();
                jfxDialog.setContent(new Label("assafsa"));
                jfxDialog.setVisible(true);
                jfxDialog.setTransitionType(JFXDialog.DialogTransition.CENTER);

                StackPane stackPane=new StackPane();
                stackPane.setPrefWidth(homeStage.root.getPrefWidth());
                stackPane.setPrefHeight(homeStage.root.getPrefHeight());
                stackPane.getChildren().add(new Label("66"));
                // 添加遮罩
                homeStage.root.getChildren().addAll(stackPane);
                jfxDialog.show(stackPane);
                jfxDialog.setOnDialogClosed(new EventHandler<JFXDialogEvent>() {
                    @Override
                    public void handle(JFXDialogEvent event) {
                        homeStage.root.getChildren().remove(stackPane);
                    }
                });*/
            }
        });
    }

}
